<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 
 * 后台管理员角色管理模型类
 * @author 66diqiu-li
 *
 */
class Admin_Adminrole_model extends CI_Model {

    /**
     * @var 表名称
     */
    private $table;
    private $sid;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->table  = $this->db->dbprefix("admin_role");
    }

    /**
     *
     * 更新数据
     * @param array $data 数据数组
     * @param array $where 更新条件
     */
    function edit_row($data,$where){
        if(!isset($where['sid'])){
            $where['sid'] = $GLOBALS['sid'];
        }
        $where = $this->set_where($where);
        $this->db->where($where, NULL, FALSE);
        return $this->db->update($this->table,$data);
    }
	
    /**
     * 添加数据
     * @param $data
     * @return bool
     */
    function add_row($data){
        if(!isset($data['sid']))$data['sid'] = $GLOBALS['sid'];
        if(is_array($data)){
            if($this->db->insert($this->table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
    }
	
    /**
     * 获取单条记录
     * @param array $where 查询条件
     * @param string $field
     * @return mixed
     */
    function get_row($where=array(),$field='*'){
        $where = $this->set_where($where);
        if(!empty($field))$this->db->select($field);
        $query = $this->db->get_where($this->table,$where,1);
        return $query->row_array();
    }
	
    /**
     *
     * 删除数据
     * @param array $where 删除条件
     */
    function remove_row($where=array()){
        $where = $this->set_where($where);
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        return $this->db->delete($this->table);
    }

    /**
     * 获取所有管理管理员角色
     * @param int $page_size 每页显示数量
     * @param int $now_page 第几页
     * @param $where 选择条件
     * @return array
     */
    function get_list($page_size=20,$now_page=1,$where){
        $where = $this->set_where($where);
        $this->db->select('id,name,status,remark,add_time,edit_time');
        $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->table,$page_size,($now_page-1)*$page_size);
        $data = array();
        foreach($query->result_array() as $row){
            $row['remove_url'] = site_url('admin/adminrole/ajax_remove?id='.$row['id']);
            $row['edit_url'] = site_url('admin/adminrole/edit?id='.$row['id']);
            $row['author_url'] = site_url('admin/adminrole/set_authority?id='.$row['id']);
            $data[] = $row;
        }
        return $data;
    }
	
    /**
     *
     * 获取总数量
     * @param array $where 查询条件
     */
    function get_count($where=array()){
        $where = $this->set_where($where);
        $this->db->where($where, NULL, FALSE);
        $this->db->select('id');
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }
	
    /**
     *
     * 获取可用角色列表
     */
    function get_usable_list(){
        $where = array('status'=>1);
        $where = $this->set_where($where);
        $this->db->where($where);
        $this->db->select('id,name');
        $query = $this->db->get($this->table);
        return $query->result_array();
    }

    /**
     * 返回查询条件
     * @param $where 查询条件
     * @return array|string
     */
    function set_where($where){
        if(is_array($where))$where['sid']=$this->sid;
        else $where=$where?$where.' AND sid='.$this->sid:'sid='.$this->sid;
        return $where;
    }
}